package pl.edu.agh.transstat.parsing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ConfigurationParser extends DefaultHandler {

	private String path;
	private String dirPath;
	private List<StatData> simulationsData;
	private StatData dataTmp;
	private ArrayList<String> paths;

	public ConfigurationParser(String path) {
		this.simulationsData = new ArrayList<StatData>();
		this.path = path;
		parseDocument();
	}

	private void parseDocument() {
		// get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
			File file = new File(path);
			InputStream inputStream = new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream, "UTF-8");

			InputSource is = new InputSource(reader);
			is.setEncoding("UTF-8");
			// get a new instance of parser
			SAXParser sp = spf.newSAXParser();
			// parse the file and also register this class for call backs
			sp.parse(is, this);

		} catch (SAXException se) {
			se.printStackTrace();
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equalsIgnoreCase(ConfigurationTags.stat)) {
			dataTmp = new StatData();
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.in)) {
			dirPath = attributes.getValue(ConfigurationTags.path);
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.params)) {
			if (attributes.getValue(ConfigurationTags.all) != null) {
				dataTmp.setAllParams(Boolean.parseBoolean(attributes.getValue(ConfigurationTags.all)));
			} else {
				dataTmp.setAllParams(false);
			}
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.param)) {
			dataTmp.addParam(attributes.getValue(ConfigurationTags.name));
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.types)) {
			if (attributes.getValue(ConfigurationTags.all) != null) {
				dataTmp.setAllFunctions(Boolean.parseBoolean(attributes.getValue(ConfigurationTags.all)));
			} else {
				dataTmp.setAllFunctions(false);
			}
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.type)) {
			dataTmp.addFunction(attributes.getValue(ConfigurationTags.name));
		}
		if (qName.equalsIgnoreCase(ConfigurationTags.stat)) {
			dataTmp.setOneChart(Boolean.parseBoolean(attributes.getValue(ConfigurationTags.oneChart)));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals(ConfigurationTags.stat)) {
			simulationsData.add(dataTmp);
		}
		
	}

	public List<StatData> getData() {
		return simulationsData;
	}
	
	public String getDirPath(){
		return dirPath;
	}

	public List<String> getPaths(String in, String end){
		paths = new ArrayList<String>();
		File root = new File(in);
		getFiles(root, end);		
		return paths;
	}
	
	private void getFiles(File file, String end){
		for (File f : file.listFiles()) {
			if(f.isDirectory()){
				getFiles(f, end);
			} else {
				String temp[] = f.getName().split("\\.");
				if (temp[temp.length-1].equals(end)){
					paths.add(f.getPath());
				}
			}
		}
		
	}

}
